//
//  BIDViewWithImageAndCloseBtn.m
//  shandongshangbao
//
//  Created by mal on 14/12/3.
//  Copyright (c) 2014年 cnsoft. All rights reserved.
//

#import "BIDViewWithImageAndCloseBtn.h"

@implementation BIDViewWithImageAndCloseBtn
@synthesize delegate;
@synthesize imgView;

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)awakeFromNib
{
    [super awakeFromNib];
    _closeBtn.layer.cornerRadius = 10;
    _closeBtn.hidden = NO;
    imgView.layer.borderColor = [UIColor blackColor].CGColor;
    imgView.layer.borderWidth = 0.5f;
    UILongPressGestureRecognizer *longPressGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureHandler:)];
    [self addGestureRecognizer:longPressGR];
}

- (IBAction)closeBtnHandler:(id)sender
{
    [self removeFromSuperview];
    [delegate toDelete:self.tag];
}

- (void)longPressGestureHandler:(UIGestureRecognizer*)gestureRecognizer
{
    [delegate tellOtherToWobble];
}

- (void)setImage:(UIImage *)img
{
    self.imgView.image = img;
}

- (void)toWobble
{
    _closeBtn.hidden = NO;
    CATransform3D transform;

    if (arc4random() % 2 == 1)
        transform = CATransform3DMakeRotation(-0.08, 0, 0, 1.0);
    else
        transform = CATransform3DMakeRotation(0.08, 0, 0, 1.0);
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
    animation.toValue = [NSValue valueWithCATransform3D:transform];
    animation.autoreverses = YES;
    animation.duration = 0.1;
    animation.repeatCount = 10000;
    animation.delegate = self;
    [[self layer] addAnimation:animation forKey:@"wobbleAnimation"];
}
- (void)toStop
{
    _closeBtn.hidden = YES;
    [[self layer] removeAnimationForKey:@"wobbleAnimation"];
}

- (id)copyWithZone:(NSZone *)zone
{
    BIDViewWithImageAndCloseBtn *newView = [[BIDViewWithImageAndCloseBtn allocWithZone:zone] init];
    newView.frame = self.frame;
    newView.tag = self.tag;
    return newView;
}

@end
